package vn.edu.hungvuongaptech.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import vn.edu.hungvuongaptech.common.Constant;
import vn.edu.hungvuongaptech.dao.TimeSheetDAO;

/**
 * Servlet implementation class UpdateTimeSheetController
 */
public class UpdateTimeSheetController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateTimeSheetController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int isSuccess = 1;
		long timesheetId = Long.parseLong(request.getParameter("txtTimeSheetId")
				.toString());
		long projectId = Long.parseLong(request.getParameter("cboProject")
				.toString());
		int productId = Integer.parseInt(request.getParameter("cboProduct")
				.toString());
		int processId = Integer.parseInt(request.getParameter("cboProcess")
				.toString());
		int workId = Integer.parseInt(request.getParameter("cboWork")
				.toString());
		String date = request.getParameter("txtDate");
		Float time = Float.parseFloat(request.getParameter("txtTime")
				.toString());
		String description = request.getParameter("txtDescription");
		if (!date.equals(null) || !description.equals(null) || time != null) {
			isSuccess = TimeSheetDAO.updateTimeSheetById(timesheetId, projectId,
					workId, processId, productId, date, time, description);
			if (isSuccess == 0) {
				response.sendRedirect(Constant.PATH_RES
						.getString("timesheet.staff.listTimeSheetPath")+"?msg=Update successfully!");
			} else {
				response.sendRedirect(Constant.PATH_RES
						.getString("timesheet.staff.updateTimeSheetPath")
						+ "?msg=Error when updating!!!");
			}
		} else {
			response.sendRedirect(Constant.PATH_RES
					.getString("timesheet.staff.updateTimeSheetPath")
					+ "?msg=you must fill all empty field");
		}
	
	}

}
